﻿#region Copyright (c) 2003 - 2008, Andreas Mueller
/////////////////////////////////////////////////////////////////////////////////////////
// 
// Copyright (c) 2003 - 2008, Andreas Mueller.
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// Contributors:
//    Andreas Mueller - initial API and implementation
//
// 
// This software is derived from software bearing the following
// restrictions:
// 
// Copyright (c) 1994
// Hewlett-Packard Company
// 
// Permission to use, copy, modify, distribute and sell this software
// and its documentation for any purpose is hereby granted without fee,
// provided that the above copyright notice appear in all copies and
// that both that copyright notice and this permission notice appear
// in supporting documentation.  Hewlett-Packard Company makes no
// representations about the suitability of this software for any
// purpose.  It is provided "as is" without express or implied warranty.
// 
// 
// Copyright (c) 1996,1997
// Silicon Graphics Computer Systems, Inc.
// 
// Permission to use, copy, modify, distribute and sell this software
// and its documentation for any purpose is hereby granted without fee,
// provided that the above copyright notice appear in all copies and
// that both that copyright notice and this permission notice appear
// in supporting documentation.  Silicon Graphics makes no
// representations about the suitability of this software for any
// purpose.  It is provided "as is" without express or implied warranty.
// 
// 
// (C) Copyright Nicolai M. Josuttis 1999.
// Permission to copy, use, modify, sell and distribute this software
// is granted provided this copyright notice appears in all copies.
// This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
// 
/////////////////////////////////////////////////////////////////////////////////////////
#endregion
using System.Collections.Generic;
using NStl.Linq;
using NUnit.Framework;
using NStl.SyntaxHelper;
using NStl.Iterators;
using System.Collections;
using NUnit.Framework.SyntaxHelpers;
using NStl.NUnit.Support;

namespace NStl.NUnit.AlgorithmFixtures.Partition
{
    [TestFixture]
    public class StablePartitionFixture : AlgorithmBaseFixture
    {
        [Test]
        public override void SuccessfullPass()
        {
            List<int> v = new List<int> {5, 2, 3, 4, 1, 0};

            IForwardIterator<int> first = v.Begin();
            IForwardIterator<int> last = v.End();

            IForwardIterator<int> middle =
                Algorithm.StablePartition(first, last, Bind.Second(Compare.LessEqual<int>(), 3));

            ICollection expectedLower = new[] { 0, 1, 2, 3 };
            ICollection expectedUpper = new[] { 4, 5 };

            ICollection actualLower = first.AsCollection(middle);
            ICollection actualUpper = middle.AsCollection(last);

            Assert.That(actualLower, Is.EquivalentTo(expectedLower));
            Assert.That(actualUpper, Is.EquivalentTo(expectedUpper));
        }
        [Test]
        public void SuccessfullPassIsStable()
        {
            List<IdentifiableInteger> v = new List<IdentifiableInteger> {5, 2, 3, 4, 1, 0};

            IForwardIterator<IdentifiableInteger> first = v.Begin();
            IForwardIterator<IdentifiableInteger> last = v.End();

            IForwardIterator<IdentifiableInteger> middle =
                Algorithm.StablePartition(first, last, Bind.Second(Compare.LessEqual<IdentifiableInteger>(), new IdentifiableInteger(3)));

            IBinaryFunction<IdentifiableInteger, IdentifiableInteger, bool> isLessById =
                Functional.PtrFun((IdentifiableInteger x, IdentifiableInteger y) => x.ID < y.ID);

            bool lowerRangeIsSortedById = Algorithm.IsSorted(first, middle, isLessById);
            bool upperRangeIsSortedById = Algorithm.IsSorted(middle, last, isLessById);

            Assert.That(lowerRangeIsSortedById, Is.True);
            Assert.That(upperRangeIsSortedById, Is.True);
        }
        [Test]
        public void PartionOnSortedRange()
        {
            List<int> v = new List<int> {0, 1, 2, 3, 4, 5};
            Algorithm.RandomShuffle(v.Begin(), v.End());

            IForwardIterator<int> first = v.Begin();
            IForwardIterator<int> last = v.End();

            IForwardIterator<int> middle =
                Algorithm.StablePartition(first, last, Bind.Second(Compare.LessEqual<int>(), 3));

            ICollection expectedLower = new[] { 0, 1, 2, 3 };
            ICollection expectedUpper = new[] { 4, 5 };

            ICollection actualLower = first.AsCollection(middle);
            ICollection actualUpper = middle.AsCollection(last);

            Assert.That(actualLower, Is.EquivalentTo(expectedLower));
            Assert.That(actualUpper, Is.EquivalentTo(expectedUpper));
        }

        [Test]
        public override void NonSuccessfullPass()
        {
            List<int> v = new List<int> {0, 1, 2, 3, 4, 5};
            
            ICollection expected = v.ToArray();

            IForwardIterator<int> first = v.Begin();
            IForwardIterator<int> last = v.End();

            IForwardIterator<int> middle =
                Algorithm.StablePartition(first, last, Bind.Second(Compare.Less<int>(), 42));

            Assert.That(middle, Is.EqualTo(last));
            Assert.That(v, Is.EqualTo(expected));
        }
        [Test]
        public override void StartIteratorIsEqualToEndIterator()
        {
            List<int> v = new List<int> {0, 3, 4, 2, 1, 5};

            IForwardIterator<int> first = v.Begin();
            
            IForwardIterator<int> middle =
                Algorithm.StablePartition(first, first, Bind.Second(Compare.Less<int>(), 42));
            Assert.That(middle, Is.EqualTo(first));
        }
    }
}
